package com.h3bpm.kumhosunny.crm.controller;

import OThinker.Common.DateTimeUtil;
import OThinker.H3.Controller.ControllerBase;
import com.h3bpm.base.res.ResBody;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataTable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @authormi
 * @date2019/11/2
 */
@Controller
@RequestMapping({"/Portal/SystemNumber"})
public class SystemNumberConteroller extends ControllerBase {
    @Override
    public String getFunctionCode() {
        return null;
    }

    @RequestMapping("/getNum")
    @ResponseBody
    public ResBody getNum(String tableCode,String proNumFild){
        String returnProjectNumber = "";
        Date date = new Date();
        String dateStr = DateTimeUtil.getYear(date) + "" + String.format("%02d", DateTimeUtil.getMonth(date));
        String querySql = " SELECT "+proNumFild+" from I_"+tableCode+" WHERE "+proNumFild+" LIKE '" + dateStr + "%' ORDER BY "+proNumFild+" DESC LIMIT 1 ";
        try {
            DataTable dataTable = SqlUtil.doQuery(querySql);
            if (dataTable.getRows() != null && dataTable.getRows().size() > 0) {
                String projectNumber = dataTable.getRows().get(0).getString(0);
                int num = Integer.valueOf(projectNumber.split(dateStr)[1]);
                returnProjectNumber = dateStr + String.format("%03d", num + 1);
            } else {
                returnProjectNumber = dateStr + "001";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResBody.buildSuccessResBody(returnProjectNumber);



    }




}
